import { ProgressBar, Tween ,Animation} from 'cc';
import { _decorator, Component, Node, Label, tween, view, UITransform, Vec3, SpriteAtlas, SpriteFrame, Sprite, Button, ScrollView, find, math, RichText, EventTouch, WebView } from 'cc';
import { App } from '../../App';
import { EventConst } from '../../common/Common';
import CommConst from '../../const/CommConst';
import wsTool from '../../net/ws';
import { PD } from '../../player/PlayerData';
import { loadCommonPreFab, showErrorTip, showRewardBox, showTip } from '../../tools/PopuUp';
import { TipMg } from '../../tools/TipMg';
import UtilTool from '../../tools/UtilTool';
const { ccclass, property } = _decorator;

// 升级球杆界面
@ccclass('UpgradeCuePanel')
export class UpgradeCuePanel extends Component {
    @property(Node)
    contentNode: Node = null;

    parentObj = null
    data = null

    dragonAnim:Animation = null

    onLoad() {
        UtilTool.nodeActive(this.contentNode, 5, 0.16)
        App.Event.on(EventConst.UPDATE_CUE_DETAIL,this.updateCueData, this )
    }

    start() {
        this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())
        this.dragonAnim = find('dragonAnim',this.contentNode).getComponent(Animation)
        this.dragonAnim.on(Animation.EventType.FINISHED, this.animFinish, this)
        this.dragonAnim.node.active = false
    }

    /**更新球杆数据 */
    updateCueData(clubList) {
        let data = null
        for (let index = 0; index < clubList.length; index++) {
            const pData = clubList[index]
            if (pData.number == this.data.number) {
                data = pData
                break
            }
        }

        if (data) {
            this.setData(this.parentObj, data)
        }
    }

    setData(parentObj, data) {
        this.parentObj = parentObj
        this.data = data
        let maxLevelClub = data.maxLevelClub    // 最大等级属性
        let nextLevelClub = data.nextLevelClub  // 下一等级属性

        let bg = find("bg",this.contentNode)
        let cueSprite = find("cueSpr",bg).getComponent(Sprite)
        let LD_ProgressBar = find("LD_ProgressBar",bg).getComponent(ProgressBar)
        let MZ_ProgressBar = find("MZ_ProgressBar",bg).getComponent(ProgressBar)
        let ML_ProgressBar = find("ML_ProgressBar",bg).getComponent(ProgressBar)
        let nameLabel = find("nameLabel",bg).getComponent(Label)
        let levelLabel = find("levelLabel",bg).getComponent(Label)
        let LD_Label_v = find("LD_Label_v",bg).getComponent(Label)
        let MZ_Label_v = find("MZ_Label_v",bg).getComponent(Label)
        let ML_Label_v = find("ML_Label_v",bg).getComponent(Label)
        let starLabel = find("starLabel",bg).getComponent(Label)
        let upgrade_btn = find("upgrade_btn",bg)
        let maxLevelLabel = find("maxLevelLabel",bg)

        // 球杆
        cueSprite.spriteFrame = this.parentObj.cueFrames[data.number]
        // 名字
        nameLabel.string = App.language.getLanguageLab(176)[data.number + ""]
        // 等级
        if (data.level==null) {
            levelLabel.node.active = false
        }
        else{
            levelLabel.node.active = true
            levelLabel.string = "Lv." + data.level
        }
        // 属性
        let LD_add = nextLevelClub.vigor-data.vigor
        let MZ_add = nextLevelClub.sightLine-data.sightLine
        let ML_add = nextLevelClub.charm-data.charm
        LD_Label_v.string = data.vigor + " (+" + LD_add + ")"
        MZ_Label_v.string = data.sightLine + " (+" + MZ_add + ")"
        ML_Label_v.string = data.charm + " (+" + ML_add + ")"
        // 星币数
        starLabel.string = PD.user.starCoin + "/" + nextLevelClub.experience

        // 进度条
        LD_ProgressBar.progress = data.vigor/maxLevelClub.vigor
        MZ_ProgressBar.progress = data.sightLine/maxLevelClub.sightLine
        ML_ProgressBar.progress = data.charm/maxLevelClub.charm

        // 是否已达最大等级
        if (data.level>=data.maxClubLevel){
            upgrade_btn.active = false
            maxLevelLabel.active = true

            LD_Label_v.string = data.vigor + ""
            MZ_Label_v.string = data.sightLine + ""
            ML_Label_v.string = data.charm + ""
        }
        else{
            upgrade_btn.active = true
            maxLevelLabel.active = false
        }
    }
        

    /**
     * 请求升级球杆
     */
    reqUpgrade() {
        wsTool.send({msgCode:CommConst.Req_UPGEADE_CUE, "number":this.data.number})
        .then((res) => {
            if(res.result == 0) {
                showTip(App.language.getLanguageLab(221))
                this.parentObj.reqListData()

                this.dragonAnim.node.active = true
                // App.Audio.playOneShot("game/coinAnim_1")
                this.dragonAnim.play('dragonAnimation5')

                // this.node.destroy();
            } else {
                showErrorTip(res.result)
            }
        })
    }

    /** 
     * 点击强化球杆
      */
    clickUpgrade() {
        App.Audio.playOneShot("click")
        if (this.data == null) return

        if (PD.user.starCoin<this.data.nextLevelClub.experience){
            showTip(App.language.getLanguageLab(220))
            return
        }

        this.reqUpgrade()
    }

    /** 
     * 关闭
      */
    clickClose() {
        App.Audio.playOneShot("click")
        this.node.destroy();
    }

    // 播放序列帧动画回调
    animFinish() {
        this.dragonAnim.node.active = false
    }

    onDestroy() {
        App.Event.off(EventConst.UPDATE_CUE_DETAIL, this.updateCueData, this)
    }
    
}

